package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.controller.vo.Result;
import com.example.entity.Interaction;
import com.example.entity.PageView;
import com.example.entity.User;
import com.example.service.IInteractionService;
import com.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 互动行为信息表 前端控制器
 * </p>
 *
 * @author yzy
 * @since 2025-08-04
 */
@RestController
@RequestMapping("/interaction")
public class InteractionController {


    @Autowired
    private IInteractionService iInteractionService;

    @Autowired
    private IUserService iuserService;

    @GetMapping("/list")
    public Result list(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                       @RequestParam(value = "productName", required = false) String productName,
                       @RequestParam(value = "type", required = false) Integer type) {
        //创建分页对象
        Page<Interaction> page = new Page<>(pageNum, pageSize);

        QueryWrapper<Interaction> queryWrapper = new QueryWrapper<>();

        if (StringUtils.hasText(productName)) {

            queryWrapper.like("product.name", productName);
        }

        if (type != null) {
            queryWrapper.like("type", type);
        }

        //执行分页查询
        Page<Interaction> page1 = iInteractionService.selectList(page, queryWrapper);
        PageView pageView = new PageView();
        pageView.setPageSize(Integer.parseInt(page1.getSize() + ""));
        pageView.setPageNum(Integer.parseInt(page1.getCurrent() + ""));
        pageView.setAllRow(page1.getTotal());
        pageView.setTotalPage(page1.getPages());
        pageView.setData(page1.getRecords());
        return Result.success(pageView);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") int id) {
        iInteractionService.removeById(id);
        return Result.success("删除成功");
    }

    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam(value = "userName") String userName,
                             @RequestParam(value = "type") Integer type,
                             @RequestParam(value = "productId") Integer productId
    ){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account",userName);
        User user = iuserService.getOne(queryWrapper);
        Integer userId=user.getId();
        return Result.success(iInteractionService.removeByNameAndType(userId,type,productId));
    }

    // 判断该用户是否收藏过该商品
    @GetMapping("/judgment")
    public Boolean judgment(@RequestParam(value = "userName") String userName,
                           @RequestParam(value = "productId") Integer productId) {
        // 根据用户名查询用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account",userName);
        User user = iuserService.getOne(queryWrapper);

        Integer userId = user.getId();
        Integer type = 1;
        //查询是否存在相同的收藏记录
        QueryWrapper<Interaction> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",userId)
        .eq("product_id",productId)
        .eq("type",type);
        Interaction interaction = iInteractionService.getOne(queryWrapper1);
        if(interaction!=null){
            //该商品已被收藏
          return true;
        }else {
            //该商品未被该用户收藏
            return false;
        }
    }


    //收藏功能 新增行为信息
    @PostMapping("collect")
    public Result collect(@RequestParam(value = "userName") String userName,
                          @RequestParam(value = "productId") Integer productId) {

        // 根据用户名查询用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account",userName);
        User user = iuserService.getOne(queryWrapper);
        Integer type = 1;//默认收藏类型
        Integer userId = user.getId();
        Interaction interaction = new Interaction();
        interaction.setUserId(userId);
        interaction.setType(type);
        interaction.setProductId(productId);
        interaction.setCreateTime(LocalDateTime.now());
        //查询是否存在相同的收藏记录 //ture表示已收藏 false表示未收藏
        Boolean judgment0 = judgment(userName,productId);
        if (!judgment0){
            iInteractionService.save(interaction);
            return Result.success("收藏成功");
        }
        return Result.failed("收藏失败");
    }
}
